package com.unhz.taigu.controller;

import com.unhz.generator.pojo.UserDict;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.utils.PageSize;
import org.slf4j.Logger;
import com.unhz.taigu.service.UserDictService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * @Author: 沈佳庆
 * @Description: 用户信息数据字典
 * @Date: Created in 下午 6:39 2018/1/23 0023
 * @Modified By:
 */
@Controller
@RequestMapping("/userDict")
public class UserDictController {

    private Logger logger = LoggerFactory.getLogger(UserDictController.class);

    @Autowired
    private UserDictService userDictService;

    /**
     * 分页查询
     * @param model
     * @param pageNo 页数
     * @param keywords 关键词
     * @return
     */
    @RequestMapping("/findAllUserDict")
    public String findAllPermission(Model model,
                                    @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                    @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords){
        //List<Permission> permissions = permissionService.selectAllPermission();
        Page<UserDict> page = userDictService.selectPageUserDict(pageNo, PageSize.pageSize,keywords);
        model.addAttribute("page",page);
        return "dict/userDict/index.jsp";
    }

    /**
     * 跳转到添加用户数据字典的页面
     * @return
     */
    @RequestMapping("/addUserDict")
    public String addUserDict(Model model){
        List<UserDict> typeList = userDictService.findAllType();
        model.addAttribute("typeList",typeList);
        return "dict/userDict/add.jsp";
    }

    /**
     * 添加用户数据字典
     * @param userDict
     * @param model
     * @return
     */
    @RequestMapping("/insert")
    public String insert(UserDict userDict, Model model){
        userDictService.insertUserDict(userDict);
        return "redirect:findAllUserDict";
    }

    /**
     * 查看某一条用户数据字典的详情
     * @param dictId
     * @param model
     * @return
     */
    @RequestMapping("/details")
    public String details(@RequestParam("dictId")Integer dictId,  Model model){
        UserDict userDict = userDictService.findDictById(dictId);
        model.addAttribute("userDict",userDict);
        return "dict/userDict/details.jsp";
    }

    /**
     * 删除某一条用户数据
     * @param dictId
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(@RequestParam("dictId")Integer dictId){
        logger.info("id为"+dictId);
        try {
            userDictService.deleteById(dictId);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }

    /**
     * 进入用户数据修改页面
     * @param dictId
     * @param model
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(@RequestParam("dictId")Integer dictId,  Model model){
        UserDict userDict = userDictService.findDictById(dictId);
        List<UserDict> typeList = userDictService.findAllType();
        model.addAttribute("userDict",userDict);
        model.addAttribute("typeList",typeList);
        return "dict/userDict/update.jsp";
    }

}
